package com.zhangzhan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.domain.commcon.App;
import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.domain.dto.SaveTagDto;
import com.zhangzhan.domain.dto.TagListBQDto;
import com.zhangzhan.domain.entity.Tag;
import com.zhangzhan.domain.vo.TagVO;
import com.zhangzhan.domain.vo.PageVO;
import com.zhangzhan.mapper.TagMapper;
import com.zhangzhan.service.TagService;
import com.zhangzhan.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-07-19 22:33:38
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    /**
     * @description 分页查询标签
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.PageVO>
     * @author 31115
     * @date 2022/12/6 14:36
     */
    @Override
    public ResponseResult<PageVO> selectPageList(Integer pageNum, Integer pageSize, TagListBQDto vo) {

        Page<Tag> tagPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StringUtils.hasText(vo.getName()),Tag::getName,vo.getName());
        wrapper.eq(StringUtils.hasText(vo.getRemark()),Tag::getRemark,vo.getRemark());
        wrapper.eq(Tag::getDelFlag,"0");

        page(tagPage, wrapper);

        List<Tag> records = tagPage.getRecords();
        List<TagListBQDto> dtoList = BeanCopyUtils.beanCopyUtil(records, TagListBQDto.class);

        return ResponseResult.okResult(new PageVO(dtoList,tagPage.getTotal()));
    }

    /**
     * @description 新增
     * @param saveTagDto
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/6 15:04
     */
    @Override
    public ResponseResult saveTag(SaveTagDto saveTagDto) {
        if (!StringUtils.hasText(saveTagDto.getName())&&!StringUtils.hasText(saveTagDto.getRemark())){
            return ResponseResult.errorResult(401,"请填写完整");
        }
        save(saveTagDto);
        return ResponseResult.okResult();
    }

    /**
     * @description 删除接口
     * @param id
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/6 15:18
     */
    @Override
    public ResponseResult deleteTag(Long id) {
        LambdaUpdateWrapper<Tag> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(id!=null, Tag::getId,id).set(Tag::getDelFlag, App.DELETE_TAG);
        update(wrapper);
        return ResponseResult.okResult();
    }

    /**
     * @description 修改前回显
     * @param id
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/6 15:36
     */
    @Override
    public ResponseResult updateTag(Long id) {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id!= null,Tag::getId,id);
        Tag tag = this.getOne(wrapper);
        TagListBQDto tagDto = new TagListBQDto();
        BeanUtils.copyProperties(tag,tagDto);
        return ResponseResult.okResult(tagDto);
    }

    /**
     * @description 修改
     * @param tagDto
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/6 15:36
     */
    @Override
    public ResponseResult updateTagNow(TagListBQDto tagDto) {
        updateById(tagDto);
        return ResponseResult.okResult();
    }

    /**
     * @description 查询所有标签接口
     * 
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.TagVO>
     * @author 31115 
     * @date 2022/12/6 16:20
     */
    @Override
    public ResponseResult<TagVO> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId,Tag::getName);
        List<Tag> list = list(wrapper);
        List<TagVO> voList = list.stream()
                .map(new Function<Tag, TagVO>() {
                    @Override
                    public TagVO apply(Tag tag) {
                        TagVO tagVO = new TagVO();
                        tagVO.setId(tag.getId());
                        tagVO.setName(tag.getName());
                        return tagVO;
                    }
                }).collect(Collectors.toList());
        return ResponseResult.okResult(voList);
    }


}

